﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Domep.Client.ApplianceServiceReference;

namespace Client
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        APIServer.APIServer APIServ;

        public String User;
        public String Password;
        public String Adresse;
        AppliancePresentation[] listAppliance;

        public MainWindow()
        {

            InitializeComponent();

            User = "toto";
            Password = "tata";

            APIServ = new APIServer.APIServer();
            APIServ.ConnectUser("h", "h", "h");

            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //LUser.Content = User;
            //LPassword.Content = Password;

            displayingLogWindow();
        }

        void displayingLogWindow()
        {
            Log logWindow = new Log(this);
            logWindow.Closed += new EventHandler(logWindow_Closed);
            this.IsEnabled = false;
            logWindow.Owner = this;
            logWindow.Show();
        }

        void logWindow_Closed(object sender, EventArgs e)
        {
            this.IsEnabled = true;

            LUser.Content = User;
            LPassword.Content = Password;

            AppliancePresentation app1 = new AppliancePresentation();
            app1.name = "four-Xaousc";
            app1.brand = "LG";
            app1.brand = "FOUR";

            AppliancePresentation app2 = new AppliancePresentation();
            app2.name = "lave linge-B";
            app2.brand = "Samsung";
            app2.brand = "lave linge";

            AppliancePresentation app3 = new AppliancePresentation();
            app3.name = "four micro.";
            app3.brand = "SEB";
            app3.brand = "FOUR";

            AppliancePresentation app4 = new AppliancePresentation();
            app4.name = "Caftiere Nespresso";
            app4.brand = "Magimix";
            app4.brand = "caftiere";

            AppliancePresentation app5 = new AppliancePresentation();
            app5.name = "QSochsdofh";
            app5.brand = "qsdqvqdsc";
            app5.brand = "qsdzecq";

            AppliancePresentation app6 = new AppliancePresentation();
            app6.name = "qsdqvqdsc";
            app6.brand = "qsdqvqdsc";
            app6.brand = "qsdqvqdsc";

            AppliancePresentation app7 = new AppliancePresentation();
            app7.name = "qsdqvqdsc";
            app7.brand = "qsdqvqdsc";
            app7.brand = "qsdqvqdsc";

            LBMenu.Items.Add(makeLIB(app1));
            LBMenu.Items.Add(makeLIB(app2));
            LBMenu.Items.Add(makeLIB(app3));
            LBMenu.Items.Add(makeLIB(app4));
            LBMenu.Items.Add(makeLIB(app5));
            LBMenu.Items.Add(makeLIB(app6));
            LBMenu.Items.Add(makeLIB(app7));
  

                listAppliance = APIServ.getListAppliances();
                LAdresse.Content = "ca connect";
                foreach (AppliancePresentation appliance in listAppliance)
                    LBMenu.Items.Add(appliance.name);

        }

       public ListBoxItem makeLIB(AppliancePresentation appliance)
        {
            ListBoxItem lbi = new ListBoxItem();
            lbi.Margin = new Thickness(3);

            // Design du fond de l'item

            Border backgound = new Border();
            backgound.Background = new SolidColorBrush(Colors.Gray);
            backgound.CornerRadius = new CornerRadius(5);
            backgound.Width = 230;

            // StackPanel général

            StackPanel SProot = new StackPanel();
            SProot.Orientation = Orientation.Horizontal;
            SProot.Margin = new Thickness(0);

            // StackPanel pour les label

            StackPanel SPLabel = new StackPanel();
            SPLabel.Orientation = Orientation.Vertical;
            SPLabel.Margin = new Thickness(0);

            // Premier label + bordure pour la date de début

            Border brdNom = new Border();
            brdNom.Background = new SolidColorBrush(Colors.LightGray);
            brdNom.CornerRadius = new CornerRadius(5);
            brdNom.Height = 25;
            brdNom.Width = 160;
            brdNom.Margin = new Thickness(3);

            Label lnNom = new Label();
            lnNom.Content = appliance.name;

            brdNom.Child = lnNom;

            // Second label + bordure pour la date de fin

            Border brdBrand = new Border();
            brdBrand.Background = new SolidColorBrush(Colors.LightGray);
            brdBrand.CornerRadius = new CornerRadius(5);
            brdBrand.Height = 25;
            brdBrand.Width = 160;
            brdBrand.Margin = new Thickness(3);

            Label lbBrand = new Label();
            lbBrand.Content = appliance.brand;

            brdBrand.Child = lbBrand;

            // Troisième labal + bordure pour le genre;

            Border brdType = new Border();
            brdType.Background = new SolidColorBrush(Colors.LightGray);
            brdType.CornerRadius = new CornerRadius(5);
            brdType.Height = 25;
            brdType.Width = 160;
            brdType.Margin = new Thickness(3);

            Label lbType = new Label();
            lbType.Content = appliance.type;

            brdType.Child = lbType;

            // Rectangle pour l'image (TODO !!!!!!!!!!!!!!!!!!!!!!)

            Rectangle image = new Rectangle();
            image.Fill = new SolidColorBrush(Colors.LightGray);
            image.Height = 50;
            image.Width = 50;
            image.Margin = new Thickness(5);
            image.RadiusX = 5;
            image.RadiusY = 5;

            // Assemblage

            SPLabel.Children.Add(brdNom);
            SPLabel.Children.Add(brdBrand);
            SPLabel.Children.Add(brdType);

            SProot.Children.Add(image);
            SProot.Children.Add(SPLabel);

            backgound.Child = SProot;

            lbi.Content = backgound;

            backgound.MouseLeftButtonDown += new MouseButtonEventHandler(backgound_MouseLeftButtonDown);
           
            return lbi;
        }

       void backgound_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
       {
           ListBoxItem Sender = (sender as Border).Parent as ListBoxItem;

           int index = LBMenu.Items.IndexOf(Sender);
           LPassword.Content = index;
       }
    } 
}
